#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>


#define DEV_STH30   "/dev/sht30"

int main(int argc, char *argv[])
{
    float temperature = 0.0;
    float humidity = 0.0;
    unsigned char data[4] = {0,};

    int rv = -1;
    int fd_sht30 = -1;

    fd_sht30 = open(DEV_STH30, O_RDONLY);
    if(fd_sht30 < 0)
    {
        printf("open %s failure.\n", DEV_STH30);
        return -1;
    }
    printf("open %s successful.\n", DEV_STH30);    

    while(1)
    {
        memset(data, 0, sizeof(data));
        rv = read(fd_sht30, data, sizeof(data));
        if(rv < 0)
        {
            printf("read data failure.\n");
        }
        else
        {
            uint16_t ST, SRH;
            // data[0] LSB , data [1] MSB
            ST = data[1];
            ST <<= 8;
            ST |= data[0];

            SRH = data[3];
            SRH <<= 8;
            SRH |= data[2];

            temperature =-45.0 + (175.0 * ((float) ST / (float) 0xFFFF));
            humidity = 100.0 * ((float) SRH / (float) 0xFFFF); 
            printf("temperature : %.4f C humidity : %.4f %%\n", temperature, humidity);
        }
        sleep(5);
    }
    close(fd_sht30);

    return 0;
}
